package com.manong.bookstore.store.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.manong.bookstore.store.entity.CategoryEntity;
import com.manong.bookstore.store.service.CategoryService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27  21:38:42
 */
@RestController
@RequestMapping("store/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
//    @RequestMapping("/list/{storeId}")
//    public R list(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request, @RequestParam Map<String, Object> params, @PathVariable("storeId") Integer storeId){
//        if (sessionId!=null){
//            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
//                PageUtils page = categoryService.queryPage(params,storeId);
//                System.out.println("category:sessionId="+request.getSession().getId());
//                System.out.println("category:cookies="+sessionId);
//                return R.ok().put("page", page);
//            }
//        }
//        return R.error("请先登录!");
//    }
    /**
     * 列表
     */
    @RequestMapping("/list/{storeId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("storeId") Integer storeId){
            PageUtils page = categoryService.queryPage(params,storeId);
            return R.ok().put("page", page);
    }


    /**
     * 信息
     */
//    @RequestMapping("/info/{categoryId}")
//    public R info(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@PathVariable("categoryId") Integer categoryId){
//        if (sessionId!=null){
//            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
//                CategoryEntity category = categoryService.getById(categoryId);
//                return R.ok().put("category", category);
//            }
//        }
//        return R.error("请先登录!");
//
//    }
    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
    public R info(@PathVariable("categoryId") Integer categoryId){
        CategoryEntity category = categoryService.getById(categoryId);
        return R.ok().put("category", category);
    }

    /**
     * 提供远程调用 根据categoryId获取目录名称
     * @param categoryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getName/{categoryId}")
    public String getName(@PathVariable("categoryId") Integer categoryId){
        CategoryEntity category = categoryService.getById(categoryId);
        return category.getCategoryName();
    }

    /**
     * 提供远程调用 根据storeId获取所有目录名称
     * @param params
     * @param storeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getNames/{storeId}")
    public Object[] getNames(@RequestParam Map<String, Object> params, @PathVariable("storeId") Integer storeId){
        PageUtils page = categoryService.queryPage(params,storeId);
        List<CategoryEntity> categoryEntities= (List<CategoryEntity>) page.getList();
        Object[] categories=new Object[categoryEntities.size()];
        int i=0;
        for(CategoryEntity categoryEntity:categoryEntities){
            Map<String, String> category=new HashMap<>();
            category.put("value",categoryEntity.getCategoryId().toString());
            category.put("text",categoryEntity.getCategoryName());
            categories[i++]=category;
        }
        return categories;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody CategoryEntity category){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                categoryService.save(category);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody CategoryEntity category){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                categoryService.updateById(category);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody Integer[] categoryIds){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                categoryService.removeByIds(Arrays.asList(categoryIds));
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

}
